home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Includes / UOSAScript.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  11.6 KB  |  368 lines  |  [TEXT/MPS ]

  1. // UOSAScript.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __UOSASCRIPT__
  5. #define __UOSASCRIPT__
  6.  
  7. // MacApp
  8.  
  9. #ifndef __UOBJECT__
  10. #include "UObject.h"
  11. #endif
  12.  
  13. #ifndef __UCOMMAND__
  14. #include "UCommand.h"
  15. #endif
  16.  
  17. // Toolbox
  18.  
  19. #ifndef __COMPONENTS__
  20. #include <Components.h>
  21. #endif
  22.  
  23. #ifndef __OSA__
  24. #include <OSA.h>
  25. #endif
  26.  
  27.  
  28. //----------------------------------------------------------------------------------------
  29. // Forward and external class declarations. 
  30. //----------------------------------------------------------------------------------------
  31.  
  32. class CAEDesc;
  33. class TAppleEvent;
  34. class TOSAScriptRep;
  35. class TFile;
  36.  
  37. //========================================================================================
  38. // COSAScript: Object representation of an OSA Script.
  39. //========================================================================================
  40.  
  41. class COSAScript
  42. {
  43.   protected:
  44.     static ComponentInstance fgGenericScriptingComponent;
  45.     static COSAScript fgGlobalScript;
  46.     static COSAScript fgSharedScript;
  47.     static COSAScript fgEmptyScript;
  48.     
  49.   public:
  50.     static ComponentInstance GetDefaultComponent();
  51.     
  52.     static COSAScript GetEmptyScript();
  53.  
  54.     inline static COSAScript GetGlobalScript();
  55.     inline static void SetGlobalScript(OSAID globalScript);
  56.     
  57.     inline static COSAScript GetSharedScript();
  58.     inline static void SetSharedScript(OSAID sharedScript);
  59.     
  60.   public:
  61.     OSAID fScriptID;
  62.  
  63.     COSAScript();
  64.     COSAScript(OSAID theScript);
  65.  
  66.     operator OSAID() const            { return fScriptID; }
  67.     operator OSAID*()                { return &fScriptID; }
  68.     
  69.     OSAID operator=(OSAID theScript)    { fScriptID = theScript; return fScriptID; }
  70.  
  71.     Boolean IsEmpty();
  72.  
  73.     OSAError Execute(ComponentInstance theComponent, OSAID theContext, long executeFlags, OSAID& theResult);
  74.     OSAError GetScript(ComponentInstance theComponent, DescType whichType, CAEDesc& scriptDesc, long storeFlags);
  75.     OSAError SetScript(ComponentInstance theComponent, const CAEDesc& scriptDesc, long compileFlags, long loadFlags);
  76.     OSAError DisposeScript(ComponentInstance theComponent);
  77.     OSAError SetParentScript(ComponentInstance genericComponent, OSAID itsParent);
  78.     OSAError GetParentScript(ComponentInstance genericComponent, OSAID& itsParent);
  79.     OSAError DoOSAEvent(ComponentInstance theComponent, TAppleEvent* message, TAppleEvent* reply, long modeFlags);
  80. };
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // COSAScript inlines
  84. //----------------------------------------------------------------------------------------
  85.  
  86. inline COSAScript COSAScript::GetGlobalScript()
  87. {
  88.     return COSAScript::fgGlobalScript;
  89. }
  90.  
  91. inline void COSAScript::SetGlobalScript(OSAID globalScript)
  92. {
  93.     COSAScript::fgGlobalScript = globalScript;
  94. }
  95.  
  96. inline COSAScript COSAScript::GetSharedScript()
  97. {
  98.     return COSAScript::fgSharedScript;
  99. }
  100.  
  101. inline void COSAScript::SetSharedScript(OSAID sharedScript)
  102. {
  103.     COSAScript::fgSharedScript = sharedScript;
  104. }
  105.  
  106. //========================================================================================
  107. // CLASS COSAScriptCntPtr
  108. //========================================================================================
  109.  
  110. class COSAScriptCntPtr
  111. {
  112.     //----------------------------------------------------------------------------------------
  113.     // Static members
  114.     //----------------------------------------------------------------------------------------
  115.  
  116. public:
  117.     static TOSAScriptRep* NewOSAScript();
  118.     
  119. public:
  120.     COSAScriptCntPtr();
  121.         // Creates a "NULL" pointer.
  122.  
  123.     virtual ~COSAScriptCntPtr();
  124.         // Destroy the pointer.  
  125.         // Decrements the count in the rep, and deletes the rep if count is zero.
  126.         
  127.     COSAScriptCntPtr(const COSAScriptCntPtr& other);
  128.         // FW_SPlatformPoint this pointer to the same rep as the other pointer points to.
  129.  
  130.     COSAScriptCntPtr(TOSAScriptRep* rep);
  131.         // Creates a pointer pointing at rep.
  132.     
  133.     COSAScriptCntPtr& operator=(const COSAScriptCntPtr& other);
  134.         // Assignment, point this ponter to the same rep as the other pointer points to.
  135.         
  136.     COSAScriptCntPtr& operator=(TOSAScriptRep* rep);
  137.         // Assignment, point this ponter to rep.
  138.         
  139.     TOSAScriptRep* operator->() const;    // Must be overlodded by subclass
  140.         // Provide access to members of rep.    
  141.         
  142.     TOSAScriptRep& operator*() const;
  143.         // Provide access to rep.
  144.         // This operator should be used only if operator->() is not sufficient.
  145.     
  146.     Boolean operator==(const COSAScriptCntPtr& other) const;
  147.         // Points to the same representation object?
  148.  
  149.     Boolean operator!=(const COSAScriptCntPtr& other) const;
  150.         // Points to a different representation object?
  151.  
  152.     operator const void*() const;
  153.         // Use to test if this is a "NULL" pointer.
  154.  
  155. protected:
  156.     void    SetRep(TOSAScriptRep* rep);
  157.     
  158.     void UpCount();
  159.         // Implementation method, increment the reference count in rep.
  160.         
  161.     void DownCount();
  162.         // Implementation method, decrement the reference count in rep.
  163.         // Deletes rep if reference count is zero.
  164.  
  165.     TOSAScriptRep *fRep;
  166. };
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // COSAScriptCntPtr::operator->
  170. //----------------------------------------------------------------------------------------
  171.  
  172. inline TOSAScriptRep* COSAScriptCntPtr::operator->() const
  173. {
  174.     return fRep;
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // COSAScriptCntPtr::operator*
  179. //----------------------------------------------------------------------------------------
  180.  
  181. inline TOSAScriptRep& COSAScriptCntPtr::operator*() const
  182. {
  183.     return *fRep;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // COSAScriptCntPtr::operator==
  188. //----------------------------------------------------------------------------------------
  189.  
  190. inline Boolean COSAScriptCntPtr::operator==(const COSAScriptCntPtr& other) const
  191. {
  192.     return (fRep == other.fRep);
  193. }
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // COSAScriptCntPtr::operator!=
  197. //----------------------------------------------------------------------------------------
  198.  
  199. inline Boolean COSAScriptCntPtr::operator!=(const COSAScriptCntPtr& other) const
  200. {
  201.     return !operator==(other);
  202. }
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // COSAScriptCntPtr::operator const void*
  206. //----------------------------------------------------------------------------------------
  207.  
  208. inline COSAScriptCntPtr::operator const void*() const
  209. {
  210.     return (const void*) fRep;
  211. }
  212.  
  213. //========================================================================================
  214. // TOSAScriptRep: MacApp Object representation of an OSA Script.
  215. //========================================================================================
  216.  
  217. class TOSAScriptRep : public TObject
  218. {
  219.     MA_DECLARE_CLASS;
  220.     
  221.     //----------------------------------------------------------------------------------------
  222.     // Instance data
  223.     //----------------------------------------------------------------------------------------
  224.  
  225.   public:
  226.     COSAScript            fScript;                // the script
  227.     long                fNextScriptIdle;        // when to next give idle time to attached scripts
  228.     long                fLoadFlags;                // Flags for OSALoad
  229.     long                fCompileFlags;            // Flags for OSACompile
  230.     long                fStoreFlags;            // Flags for OSAStore
  231.     long                fExecuteFlags;            // Flags for OSAExecute
  232.     long                fDoEventFlags;            // Flags for OSADoEvent    
  233.     ComponentInstance    fScriptingComponent;    // the scripting component for this script
  234.  
  235.     //----------------------------------------------------------------------------------------
  236.     // Construction
  237.     //----------------------------------------------------------------------------------------
  238.  
  239.   public:
  240.     
  241.     TOSAScriptRep();
  242.     virtual ~TOSAScriptRep();
  243.     
  244.     void IOSAScript();
  245.  
  246.     //----------------------------------------------------------------------------------------
  247.     // Attached Scripts
  248.     //----------------------------------------------------------------------------------------
  249.  
  250.     virtual Boolean HasOSAScript();
  251.         // Return TRUE if this object has a script attached.
  252.  
  253.     virtual void FreeOSAScript();
  254.         // Throws out the script attached to this object.
  255.  
  256.     //    virtual ComponentInstance GetScriptingComponent();
  257.     //        // Returns an instance of the scripting component used with scripts
  258.     //        // attached to this object. Default is to return the Generic component.
  259.  
  260.     virtual void SetOSAScript(const CAEDesc& scriptDesc);
  261.         // Attaches a script described in the scriptDesc
  262.  
  263.     virtual void GetOSAScript(CAEDesc& scriptDesc,
  264.                               const CAEDesc& desiredType);
  265.         // Returns the script attached to this object as the desiredType.
  266.  
  267.     virtual OSAID ExecuteOSAScript();
  268.         // Executes the attached script and returns the result.
  269.     
  270.     virtual Boolean HandleOSAEvent(CommandNumber aCommandNumber,
  271.                                     TAppleEvent* message,
  272.                                     TAppleEvent* reply);
  273.         // Gives an attached script a chance to handle an AppleEvent.
  274.         // Returns TRUE if the script handled the event.
  275.  
  276.     virtual void IdleOSAScript();
  277.         // Gives an attached script idle time so its idle event handler can do some work.
  278.         // Respects the object's fNextScriptIdle so you can call it as often as you like.
  279.  
  280.     virtual OSAID GetOSAParent();
  281.         // Returns this object's parent script. If the scripting component supports
  282.         // inheritance the HandleOSAEvent method will use this parent script to
  283.         // set up the inheritance chain.
  284.         // The default parent script is TScriptEventDispatcher's shared handler script.
  285.  
  286.     virtual void ReadOSAScript(TStream* aStream);
  287.         // Reads a script from a stream.
  288.  
  289.     virtual void ReadOSAScript(TFile* aFile,
  290.                                 ResType resourceType,
  291.                                 short resourceID);
  292.         // Reads a script from a resource
  293.     
  294.     virtual void WriteOSAScript(TStream* aStream, DescType asType);
  295.         // Writes a script to aStream.
  296.     
  297.     virtual void WriteOSAScript(TFile* aFile, 
  298.                                 DescType theType,
  299.                                 short resourceID,
  300.                                 CStr255& resourceName);
  301.         // Writes an attached script to aStream.
  302.  
  303.     //----------------------------------------------------------------------------------------
  304.     // Reference Counting Support - We need reference counting in order to prevent
  305.     //    premature death of a script while it is still being executed.  This could
  306.     //    happen if the object to which the script is attached is killed off while
  307.     //    the script is being run (such as in an "on close" attached script).
  308.     //----------------------------------------------------------------------------------------
  309.     
  310.     long    IncrementReferenceCount();
  311.     long    DecrementReferenceCount();
  312.  
  313. protected:
  314.     unsigned long     fRefCount;
  315. };
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // TOSAScriptRep inlines
  319. //----------------------------------------------------------------------------------------
  320.  
  321. inline long TOSAScriptRep::DecrementReferenceCount()
  322. {
  323.     return --fRefCount;
  324. }
  325.  
  326. inline long TOSAScriptRep::IncrementReferenceCount()
  327. {
  328.     return ++fRefCount;
  329. }
  330.  
  331. //========================================================================================
  332. // TExecuteScriptCommand: Command to execute an OSA script.
  333. //========================================================================================
  334.  
  335. class TExecuteScriptCommand : public TCommand
  336. {
  337.     MA_DECLARE_CLASS;
  338.     
  339. public:
  340.     COSAScript    fScript;
  341.     COSAScript    fScriptContext;
  342.     Boolean        fDisposeScript;
  343.     ComponentInstance    fComponent;
  344.     long        fExecuteFlags;
  345.  
  346.     TExecuteScriptCommand();
  347.         // Constructor
  348.         
  349.     void IExecuteScriptCommand(CommandNumber itsCommandNumber,
  350.                                TCommandHandler* itsContext,
  351.                                Boolean canUndo,
  352.                                Boolean causesChange,
  353.                                TObject* objectToNotify,
  354.                                OSAID scriptToExecute,
  355.                                OSAID theScriptContext,
  356.                                long executeFlags,
  357.                                Boolean disposeOfScript,
  358.                                ComponentInstance whichComponent);
  359.  
  360.     virtual void DoIt();    
  361.         // Executes the script
  362.  
  363.     virtual ~TExecuteScriptCommand();
  364.         
  365. };
  366.  
  367. #endif // __UOSASCRIPT__
  368.